#include "comboxdelegate2.h"


ComBoxDelegate2::ComBoxDelegate2( QObject * parent )
    : QItemDelegate(parent)
{
}

QWidget * ComBoxDelegate2::createEditor( QWidget * parent,
                                         const QStyleOptionViewItem &,
                                         const QModelIndex & ) const
{
QIcon green = QIcon( QPixmap(":/binary/green_b.png") );
QIcon red = QIcon(QPixmap(":/binary/red_b.png") );

QComboBox * editor = new QComboBox(parent);
editor->setMaxVisibleItems(2);

QString hide = tr("Hide");
editor->insertItem( 0, red, hide );

QString show = tr("Show");
editor->insertItem( 1, green, show );

editor->installEventFilter( const_cast<ComBoxDelegate2 *>(this) );
return editor;
}

void ComBoxDelegate2::setModelData( QWidget * editor,
                                    QAbstractItemModel * model,
                                    const QModelIndex & index ) const
{
QComboBox * comboBox = static_cast<QComboBox *>(editor);
int value = comboBox->currentIndex();
   if ( value == 1 )
   {
   model->setData( index, 1 );
   }
   else
   {
   model->setData( index, 0 );
   }
}

void ComBoxDelegate2::setEditorData( QWidget * editor,
                                     const QModelIndex & index ) const
{
    if ( index.model() == NULL )
    {
    return;
    }

QString value = index.model()->data(index, Qt::DisplayRole).toString();
QString show = tr("Show");
QComboBox * comboBox = static_cast<QComboBox *>(editor);
    if ( value == show )
    {
    comboBox->setCurrentIndex(1);
    }
    else
    {
    comboBox->setCurrentIndex(0);
    }
}

